<template>
    <div class="search-panel">
        <el-row class="m-header-searchbar">
            <el-col :span="3" class="left">
                <img src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png" alt="美团" @click="jump">
            </el-col>
            <el-col :span="15" class="center">
                <div class="wrapper">
                    <el-input @input="Input" @focus="Focus" @blur="Blur" v-model="search" placeholder="搜索商家或地点" />
                    <button class="el-button el-button-primary"><i class="el-icon-search"/></button>
                    <dl v-if="isHOtPlace" class="hotPlace">
                        <dt>热门搜索</dt>
                        <dd v-for="(item,index) of $store.state.home.hotPlace.slice(0,4)" :key="index">
                            <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
                        </dd>
                    </dl>
                    <dl v-if="isSearchList" class="searchList">
                        <dd v-for="(item,index) of searchList" :key="index">
                            <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
                        </dd>
                    </dl>
                </div>
                <p class="suggest" >
                    <a :href="'/products?keyword='+encodeURIComponent(item.name)" v-for="(item,index) of $store.state.home.hotPlace.slice(0,6)" :key="index">{{item.name}}</a>
                </p>
                <ul class="nav">
                    <li>
                        <nuxt-link to="/" class="takeout">美团外卖</nuxt-link>
                    </li>
                    <li>
                        <nuxt-link to="/" class="movie">猫眼电影</nuxt-link>
                    </li>
                    <li>
                        <nuxt-link to="/" class="hotel">美团酒店</nuxt-link>
                    </li>
                    <li>
                        <nuxt-link to="/" class="apartment">民宿/公寓</nuxt-link>
                    </li>
                    <li>
                        <nuxt-link to="/" class="ruzhu">商家入驻</nuxt-link>
                    </li>
                    <li>
                        <nuxt-link to="/" class="free">美团公益</nuxt-link>
                    </li>
                </ul>
            </el-col>
            <!-- <el-col :span="6" class="right">
                3
            </el-col> -->
        </el-row>
    </div>
</template>

<script>
import _ from 'lodash'
export default {
    data() {
        return {
            searchFocuse: false,
            search: '',
            // hotPlace: ['故宫博物院','故宫珍宝馆','怀北国际滑雪场','第九届陶然亭冰雪嘉年华'],
            hotPlace: ['故宫博物院','故宫珍宝馆','怀北国际滑雪场','第九届陶然亭冰雪嘉年华'],
            searchList: ['火锅','火锅','火锅','火锅','火锅','火锅','火锅','火锅']
        }
    },
    computed: {
        isHOtPlace() {
            return this.searchFocuse && !this.search 
        },
        isSearchList() {
            return this.searchFocuse && this.search
        }
    },
    methods: {
        Focus() {
            this.searchFocuse = true
        },
        Blur() {
            let _this = this
            setTimeout(() => {
                _this.searchFocuse = false
            },200)
        },
        // 这就变成了一个延时函数
        Input: _.debounce(async function() {
            let _this = this
            const city = _this.$store.state.geo.position.city.replace('市','')
            let searchList = []
            this.$axios.get('/search/top', {params: {input: _this.search,city,}}).then((res) => {
                _this.searchList = res.data.top.slice(0,10)
            })
        },300),
        jump() {
            window.location.href = '/'
        }
    }
}
</script>

<style lang="scss">
    .search-panel {
        height: 157px;
        display: flex;
        align-items: flex-start;
        padding: 20px;
        background: #fff;
        box-sizing: border-box;
        .left {
            width: 280px;
            padding-top: 15px;
            img {
                width: 126px;
                height: 46px;
            }
        }
        .center {
            margin-top: -10px;
            flex: 1;
            .wrapper {
                margin-top: 16px;
                border: 1px solid #13D1BE;
                border-radius: 4px;
                width: 552px;
                box-sizing: border-box;
                position: relative;
                white-space: nowrap;
                el-input {
                    width: 462px;
                }
                input {
                    border: none;
                    border-top-right-radius: 0;
                    border-bottom-right-radius: 0;
                    font-size: 15px;
                }
                .el-button {
                    width: 88px;
                    height: 41px;
                    border: none;
                    background: #13D1BE;
                    font-size: 16px;
                    border-top-left-radius: 0;
                    border-bottom-left-radius: 0;
                    // vertical-align: -1px;
                    position: absolute;
                    top: 0;
                    right: 0;
                    color: #fff;
                    i {
                        font-weight: bold;
                    }
                }
                .hotPlace,
                .searchList {
                    position: absolute;
                    top: 42px;
                    left: -1px;
                    padding: 10px;
                    background: #fff;
                    font-size: 12px;
                    width: 465px;
                    box-sizing: border-box;
                    border: 1px solid #E5E5E5;
                    border-top: none;
                    z-index: 999;
                    box-shadow: 0 3px 5px 0 rgba(0, 0, 0, .1);
                    border-bottom-left-radius: 4px;
                    border-bottom-right-radius: 4px;
                    dt {
                        margin-top: -10px;
                        font-weight: bold;
                        color: #999;
                        height: 20px;
                    }
                    dd {
                        display: inline-block;
                        color: #666;
                        margin-right: 10px;
                        margin-bottom: 3px;
                        margin-top: 5px;
                        cursor: pointer;
                        &:hover {
                            color: #31BBAC;
                        }
                    }
                    &.searchList {
                        padding: 0;
                        margin: 0;
                        dd {
                            margin: 0;
                            padding: 3px 10px;
                            display: block;
                            line-height: 1.6;
                            &:hover {
                            color: #31BBAC;
                            background: #F8F8F8;
                        }
                        }
                    }
                }
            }
            .suggest {
                width: 550px;
                padding-left: 15px;
                box-sizing: border-box;
                font-size: 12px;
                padding-top: 5px;
                a {
                   color: #999;
                   margin-right: 5px;
                   margin-top: 5px;
                   display: inline-block;
                   height: 16px;
                   line-height: 16px;
                   &:hover {
                       color: #31BBAC;
                   }
                }
            }
            .nav {
                list-style: none;
                display: flex;
                flex-direction: row;
                margin-top: 10px;
                li {
                    font-weight: 700;
                    font-size: 16px;
                    margin-right:40px;
                    cursor: pointer;
                    position: relative;
                    a {
                        color: #222;
                        &.takeout:hover {
                            color: #fbc700;
                        }
                        &.movie:hover {
                            color: #ed1e24;
                        }
                        &.hotel:hover {
                            color: #f04d4e
                        }
                        &.apartment:hover {
                            color: #FDC411;
                        }
                        &.ruzhu:hover {
                            color: #31BBAC;
                        }
                        &.free:hover {
                            color: #f04d4e;
                        }
                    }
                }
            }
        }
    }
</style>


